package com.bootdo.cms.service.impl;

import com.bootdo.cms.dao.CmsFilieDao;
import com.bootdo.cms.domain.CmsFileDO;
import com.bootdo.cms.service.ChannelService;
import com.bootdo.cms.service.CmsFileService;
import com.bootdo.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author SiMan
 * @date 2019/12/24 1:06
 */
@Service
public class CmsFileServiceImpl implements CmsFileService {
	@Autowired
	CmsFilieDao cmsFilieDao;
	@Autowired
	ChannelService channelService;

	@Override
	public List<CmsFileDO> getAll() {
		return cmsFilieDao.getAll();
	}

	@Override
	public CmsFileDO selectById(String id) {
		return cmsFilieDao.selectById(id);
	}

	@Override
	public Integer insert(CmsFileDO fileDO) {
		return cmsFilieDao.insert(fileDO);
	}

	@Override
	public Integer remove(String id) {
		return cmsFilieDao.remove(id);
	}

	@Override
	public List<CmsFileDO> selectByChannelId(String type) {
		return cmsFilieDao.selectByChannelId(type);
	}

	@Override
	public List<CmsFileDO> list(Map<String, Object> map){
		String channelId = null;
		try {
			channelId = map.get("channelId").toString();
			if (StringUtils.isNotBlank(channelId)) {
				Long channelIdL = Long.valueOf(channelId);
				List<Long> childIds = channelService.listChildrenIds(channelIdL);
				childIds.add(channelIdL);
				map.put("channelId", null);
				map.put("channelIds",childIds);
			}
		} catch (Exception e) {
			System.out.println("channelId is Null!");
		}
		return cmsFilieDao.list(map);
	}

	@Override
	public int count(Map<String, Object> map) {
		return cmsFilieDao.count(map);
	}

	@Override
	public List<CmsFileDO> selectBySearch(String fileName) {
		return cmsFilieDao.selectBySearch(fileName);
	}
}
